package com.chookss.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.tools.Utils;
import com.chookss.view.listener.ShareChoiceDialogListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.NonNull;


public class DocunmentTypeDialog extends Dialog {
    private RecyclerView recyclerView;
    private Adapter adapter;
    private List<Map<String, String>> lists = new ArrayList<>();
    private Context context;
    private ShareChoiceDialogListener listener = null;
    private int index = 0;

    public DocunmentTypeDialog(@NonNull Context context, List<Map<String, String>> data, ShareChoiceDialogListener listener) {
        super(context);
        this.context = context;
        this.lists = data;

        this.listener = listener;
        initLayoutRes();
    }

    public DocunmentTypeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initLayoutRes();
    }

    public void setSelect(int position) {
        this.index = position;
    }

    private void initLayoutRes() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_docunment_type, null);
        recyclerView = view.findViewById(R.id.lvBottom);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager
                .VERTICAL, false));
        recyclerView.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.HORIZONTAL, Utils.dip2px(context, 0.5f), context.getResources().getColor(R.color.line_gray)));

        adapter = new Adapter(R.layout.item_document_type, context, lists);
        recyclerView.setAdapter(adapter);
        setContentView(view);
        initBottom();
    }

    protected void initBottom() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        Window wm = getWindow();
        lp.width = lp.MATCH_PARENT;
        lp.height = lp.WRAP_CONTENT;
        lp.dimAmount = 0.6f;
        wm.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        wm.setGravity(Gravity.BOTTOM);
        wm.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled);
        getWindow().setAttributes(lp);
    }

    protected void safeDismiss() {
        if (isShowing()) {
            dismiss();
        }
    }

    class Adapter extends BaseQuickAdapter<Map<String, String>, BaseViewHolderEx> {
        private Context mContext;
        private ArrayList<NavigationEntity> data;

        public Adapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<Map<String, String>> data) {
            super(layoutResId, data);
            this.mContext = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, Map<String, String> entity) {
            ImageView ivType = helper.getView(R.id.ivType);
            TextView tvTitle = helper.getView(R.id.tvTitle);
            tvTitle.setText(entity.get("name"));
            if (index == helper.getItemPosition()) {
                tvTitle.setTextColor(mContext.getResources().getColor(R.color.main_color));
                if ("1".equals(entity.get("type"))) {
                    Glide.with(mContext).load(R.drawable.iv_paixu_select_down).into(ivType);
                } else {
                    Glide.with(mContext).load(R.drawable.iv_paixu_select_up).into(ivType);
                }
            } else {
                if ("1".equals(entity.get("type"))) {
                    Glide.with(mContext).load(R.drawable.iv_paixu_down_26).into(ivType);
                } else {
                    Glide.with(mContext).load(R.drawable.iv_paixu_up_26).into(ivType);
                }
                tvTitle.setTextColor(mContext.getResources().getColor(R.color.text_gray3));
            }
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (listener != null) {
                        if(helper.getItemPosition()==index){
                            if ("1".equals(entity.get("type"))) {
                                lists.get(helper.getItemPosition()).put("type", "0");
                                listener.onClick(helper.getItemPosition() * 2);
                            } else {
                                lists.get(helper.getItemPosition()).put("type", "1");
                                listener.onClick((helper.getItemPosition() * 2) + 1);
                            }
                        }else{
                            index=helper.getItemPosition();
                            if ("1".equals(entity.get("type"))) {
                                listener.onClick((helper.getItemPosition() * 2) + 1);
                            } else {
                                listener.onClick(helper.getItemPosition() * 2);
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }
}
